d1 <- data.frame(id = 10000 + as.numeric(rownames(d.kn)))
d1$congressional.district <- d.kn$geo.grp

# Willingness to participate, treatment assignment, and compliance
d1$willing.to.participate <- 1 * (d.kn$slfrpt %in% 1:2)
d1$assigned.to.info.only <- 1 * (d.kn$exp.grp %in% 4)
d1$assigned.to.session <- 1 * (d.kn$exp.grp %in% 1:2)
d1$attended.session <- 1 * (d.kn$attendance == 1)
.ok <- (d1$assigned.to.info.only == 0 & d1$assigned.to.session == 0) | 
       d1$willing.to.participate == 0
d1$assigned.to.session[.ok] <- d1$attended.session[.ok] <- NA

# Demographics
d1$Female <- 1 * (d.kn$ppgender == 2)
d1$Age <- d.kn$ppage
d1$Education <- d.kn$ppeducat # education == 1 for no HS, 2 for HS, 3 for some college, 4 for college+
d1$White <- 1 *(d.kn$ppethm == 1)
d1$Black  <- 1 *(d.kn$ppethm == 2)
d1$Latino <- 1 *(d.kn$ppethm == 4)
d1$Other <- 1 *(d.kn$ppethm %in% c(3, 5))
d1$Income <- NA
d1$Income[d.kn$ppincimp %in% 1:3] <- 1 # less than $10k
d1$Income[d.kn$ppincimp %in% 4:5] <- 2 # $10k to $15k
d1$Income[d.kn$ppincimp == 6] <- 3 # $15k to $20k
d1$Income[d.kn$ppincimp == 7] <- 4 # $20k to $25k
d1$Income[d.kn$ppincimp == 8] <- 5 # $25k to $30k
d1$Income[d.kn$ppincimp %in% 9:10] <- 6 # $30k to $40k
d1$Income[d.kn$ppincimp == 11] <- 7 # $40k to $50k
d1$Income[d.kn$ppincimp %in% 12:15] <- 8 # $50k to $100k
d1$Income[d.kn$ppincimp %in% 16:19] <- 10 # $100k or more

# Party/Position groups
d1$Party7pt <- d.kn$partyid7
.constituent.rep <- d.kn$partyid3 == 1
.constituent.dem <- d.kn$partyid3 == 3
.constituent.rep[is.na(d.kn$partyid3)] <- 0
.constituent.dem[is.na(d.kn$partyid3)] <- 0
.member.rep <- d.kn$geo.grp %in% c(3, 7, 8, 12, 16)
.member.dem <- !.member.rep
d1$same.party <- 1 * ((.constituent.rep & .member.rep) | (.constituent.dem & .member.dem))
.anti.immig <- d.kn$geo.grp %in% c(3, 7, 12, 13, 16)
.pro.immig <- !.anti.immig

# Path to citizenship
d1$path.to.citizenship.baseline <- with(d.kn,
    6 * (bas32 == 1 & bas32c == 2) +
    5 * (bas32 == 1 & bas32c == 1) +
    4 * (bas32 == 3 & bas32b == 1) +
    3 * (bas32 == 3 & bas32b == 3) +
    2 * (bas32 == 3 & bas32b == 2) +
    1 * (bas32 == 2 & bas32c == 1) +
    0 * (bas32 == 2 & bas32c == 2))
d1$path.to.citizenship.baseline[.anti.immig] <- 6 - d1$path.to.citizenship.baseline[.anti.immig]
d1$path.to.citizenship.followup <- with(d.kn,
    6 * (fol32 == 1 & fol32c == 2) +
    5 * (fol32 == 1 & fol32c == 1) +
    4 * (fol32 == 3 & fol32b == 1) +
    3 * (fol32 == 3 & fol32b == 3) +
    2 * (fol32 == 3 & fol32b == 2) +
    1 * (fol32 == 2 & fol32c == 1) +
    0 * (fol32 == 2 & fol32c == 2))
d1$path.to.citizenship.followup[.anti.immig] <- 6 - d1$path.to.citizenship.followup[.anti.immig]

# Agree on Increase Legal Immigration
d1$legal.immigration.baseline <- with(d.kn,
    4 * (bas30a == 1 & bas30b == 2) +
    3 * (bas30a == 1 & bas30b == 1) +
    2 * (bas30a == 2) +
    1 * (bas30a == 3 & bas30b == 1) +
    0 * (bas30a == 3 & bas30b == 2))
d1$legal.immigration.baseline[.anti.immig] <- 4 - d1$legal.immigration.baseline[.anti.immig]
d1$legal.immigration.followup <- with(d.kn,
    4 * (fol30a == 1 & fol30b == 2) +
    3 * (fol30a == 1 & fol30b == 1) +
    2 * (fol30a == 2) +
    1 * (fol30a == 3 & fol30b == 1) +
    0 * (fol30a == 3 & fol30b == 2))
d1$legal.immigration.followup[.anti.immig] <- 4 - d1$legal.immigration.followup[.anti.immig]

# Trust MOC
d1$trust.baseline <- 3 - (d.kn$bas17 - 1)
d1$trust.baseline[d.kn$bas17 == 5] <- NA  # set DK's to unobserved
d1$trust.followup <- 3 - (d.kn$fol17 - 1)
d1$trust.followup[d.kn$fol17 == 5] <- NA  # set DK's to unobserved

# Approve MOC
d1$approval.baseline <- 4 - (d.kn$bas22 - 1)
d1$approval.baseline[d.kn$bas22 == 7] <- NA # set DK's to unobserved
d1$approval.followup <- 4 - (d.kn$fol22 - 1)
d1$approval.followup[d.kn$fol22 == 7] <- NA # set DK's to unobserved

# Intent to Vote
d1$turnout.baseline <- 4 - (d.kn$bas20 - 1)
d1$turnout.baseline[d.kn$bas20 %in% 6:7] <- 0
d1$vote.baseline <- 4 - (d.kn$bas21 - 1)
d1$vote.baseline[d.kn$bas21 %in% 6:7] <- 0
d1$vote.followup <- 4 - (d.kn$fol21 - 1)
d1$vote.followup[d.kn$fol21 %in% 6:7] <- 0

# Actual Vote
d1$vote.november <- 1 * (d.kn$nov3 == 2 & d.kn$nov5 == 1)
d1$turnout.november <- 1 * (d.kn$nov3 == 2)

# Drop 'true controls' who were not assigned to either attend session or receive information
d1 <- subset(d1, !is.na(attended.session))

# recode baseline on 0-1 scale (to estimate Null SDs for Cohen's d's)
d1$path.to.citizenship.baseline.01 <- scale01(d1$path.to.citizenship.baseline)
d1$legal.immigration.baseline.01 <- scale01(d1$legal.immigration.baseline)
d1$trust.baseline.01 <- scale01(d1$trust.baseline)
d1$approval.baseline.01 <- scale01(d1$approval.baseline)
d1$vote.baseline.01 <- scale01(d1$vote.baseline)
d1$turnout.baseline.01 <- scale01(d1$turnout.baseline)

# recode baseline to include missingness, then create factor
d1$path.to.citizenship.baseline.NArecode <- NArecode(d1$path.to.citizenship.baseline)
d1$legal.immigration.baseline.NArecode <- NArecode(d1$legal.immigration.baseline)
d1$trust.baseline.NArecode <- NArecode(d1$trust.baseline)
d1$approval.baseline.NArecode <- NArecode(d1$approval.baseline)
d1$vote.baseline.NArecode <- NArecode(d1$vote.baseline)
d1$turnout.baseline.NArecode <- NArecode(d1$turnout.baseline)

# put responses on 0-1 scale
d1$path.to.citizenship.followup.01 <- scale01(d1$path.to.citizenship.followup)
d1$legal.immigration.followup.01 <- scale01(d1$legal.immigration.followup)
d1$trust.followup.01 <- scale01(d1$trust.followup)
d1$approval.followup.01 <- scale01(d1$approval.followup)
d1$vote.followup.01 <- scale01(d1$vote.followup)
d1$vote.november.01 <- scale01(d1$vote.november)
d1$turnout.november.01 <- scale01(d1$turnout.november)

# Calculate inverse probability of response weights
prob <- glm(is.na(path.to.citizenship.followup) ~ 
            path.to.citizenship.baseline.NArecode + 
            assigned.to.session + same.party, 
            data = d1, family = binomial)$fitted.values
d1$ipw.path.to.citizenship.followup <- 1 / prob
prob <- glm(is.na(legal.immigration.followup) ~ 
            legal.immigration.baseline.NArecode + 
            assigned.to.session + same.party, 
            data = d1, family = binomial)$fitted.values
d1$ipw.legal.immigration.followup <- 1 / prob
prob <- glm(is.na(trust.followup) ~ 
            trust.baseline.NArecode + 
            assigned.to.session + same.party, 
            data = d1, family = binomial)$fitted.values
d1$ipw.trust.followup <- 1 / prob
prob <- glm(is.na(approval.followup) ~ 
            approval.baseline.NArecode + 
            assigned.to.session + same.party, 
            data = d1, family = binomial)$fitted.values
d1$ipw.approval.followup <- 1 / prob
prob <- glm(is.na(vote.followup) ~ 
            vote.baseline.NArecode + 
            assigned.to.session + same.party, 
            data = d1, family = binomial)$fitted.values
d1$ipw.vote.followup <- 1 / prob
prob <- glm(is.na(vote.november) ~ vote.baseline.NArecode + assigned.to.session 
            + same.party, data = d1, family = binomial)$fitted.values
d1$ipw.vote.november <- 1 / prob
prob <- glm(is.na(turnout.november) ~ turnout.baseline.NArecode + assigned.to.session 
            + same.party, data = d1, family = binomial)$fitted.values
d1$ipw.turnout.november <- 1 / prob
